package com.eastfair.imaster.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.auth.vo.UserAccountVo;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import com.eastfair.imaster.dto.*;
import com.eastfair.imaster.entity.BusinResource;
import com.eastfair.imaster.entity.BusinessCircle;
import com.eastfair.imaster.service.BusinResourceService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.imaster.vo.BusinResourceVO;
import com.eastfair.imaster.vo.BusinessCircleVO;
import com.eastfair.projectcore.vo.ExhibitionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 业务资源表
 * </p>
 *
 * @author ligang
 * @date 2022-01-20
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/businResource")
@Api(value = "BusinResource", tags = "业务资源表")
public class BusinResourceController extends SuperController<BusinResourceService, Long, BusinResource, BusinResourcePageQuery, BusinResourceSaveDTO, BusinResourceUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<BusinResource> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<BusinResource> businResourceList = list.stream().map((map) -> {
            BusinResource businResource = BusinResource.builder().build();
            //TODO 请在这里完成转换
            return businResource;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(businResourceList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<BusinResourcePageQuery> params) {
        BusinResourcePageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }


    /**
     * 新增或修改公司新闻
     *
     * @param businResourceDTO 参数
     */
    @ApiOperation(value = "新增或修改公司新闻", notes = "新增公司新闻")
    @PostMapping("/saveAndUpdNews")
    public R<BusinResourceVO> saveNews(@RequestBody @Validated({BusinResourceDTO.saveNews.class})  BusinResourceDTO businResourceDTO) {
        return success(this.baseService.executeSave(businResourceDTO));
    }
    /**
     * 新增或修改公司视频
     *
     * @param businResourceDTO 参数
     */
    @ApiOperation(value = "新增或修改公司视频", notes = "新增或修改公司视频")
    @PostMapping("/saveAndUpdVideo")
    public R<BusinResourceVO> saveVideo(@RequestBody @Validated({BusinResourceDTO.saveVideo.class})  BusinResourceDTO businResourceDTO) {
        return success(this.baseService.executeSave(businResourceDTO));
    }

    /**
     * 新增或修改公司资料
     *
     * @param businResourceDTO 参数
     */
    @ApiOperation(value = "新增或修改公司资料", notes = "新增或修改公司资料")
    @PostMapping("/saveAndUpdComData")
    public R<BusinResourceVO> saveComData(@RequestBody @Validated({BusinResourceDTO.saveData.class})  BusinResourceDTO businResourceDTO) {
        return success(this.baseService.executeSave(businResourceDTO));
    }
    /**
     * 更新
     *
     * @param businResourceDTO 参数
     */
    @ApiOperation(value = "更新", notes = "更新")
    @PutMapping("/updateById")
    public R<BusinResourceVO> updateById(@RequestBody BusinResourceDTO businResourceDTO) {
        return success(this.baseService.executeUpdate(businResourceDTO));
    }


    /**
     * 分页查询
     *
     * @param params 参数
     * @return 分页数据
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/queryPage")
    public R<Page<BusinResourceVO>> queryPage(@RequestBody PageParams<BusinResourcePageQuery> params) {
//        QueryWrapper<BusinResource> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda()
//                .eq(BusinResource::getProjectId, ContextUtil.getProjectId())
//                .eq(BusinResource::getIsDeleted, BusinessConstant.DELETE_NO)
//                .orderByDesc(BusinResource::getSequence);

        BusinResource businResource = BeanUtil.toBean(params.getModel(), getEntityClass());
        QueryWrap<BusinResource> wrapper = handlerWrapper(businResource, params);

        Page<BusinResource> page = this.baseService.page(params.buildPage(), wrapper);
        Page<BusinResourceVO> voPage = ConvertUtil.convertPage(page, BusinResourceVO.class);
        List<BusinResourceVO> voList = voPage.getRecords();
        if (voList.isEmpty()) {
            return success(voPage);
        }
        this.baseService.assembleBusinResourceList(voList);
        voPage.setRecords(voList);
        return success(voPage);
    }

    /**
     * 查询资源信息
     *
     * @return
     */
    @ApiOperation(value = "查询资源信息", notes = "查询资源信息")
    @GetMapping("/getById/{id}")
    public R<BusinResourceVO> getById(@PathVariable(value = "id") Long id) {
        return R.success(baseService.findById(id));
    }


    /**
     * 审核
     *
     * @param businResourceDTO 参数
     * @return 分页数据
     */
    @ApiOperation(value = "审核", notes = "审核")
    @PostMapping("/checkResource")
    @PreAuth({"company:auth"})
    public R checkResource(@RequestBody BusinResourceDTO businResourceDTO) {
        this.baseService.checkResource(businResourceDTO.getIds(), businResourceDTO.getIsCheck());
        return R.success();
    }

    /**
     * 上下架
     *
     * @param businResourceDTO 参数
     * @return 分页数据
     */
    @ApiOperation(value = "上下架", notes = "审核")
    @PostMapping("/upResource")
    public R upResource(@RequestBody BusinResourceDTO businResourceDTO) {

        this.baseService.upResource(businResourceDTO.getIds(), businResourceDTO.getIsShelves());
        return R.success();
    }
}
